package pers.lan.stack;

import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/3 8:06 AM
 */
public class LC_1047 {

    public static void main(String[] args) {
        System.out.println(
                new LC_1047().removeDuplicates(
                        "aaaaa"
                )
        );
    }
    public String removeDuplicates(String S) {
        Stack<Character> stack = new Stack<>();
        char[] chars = S.toCharArray();
        stack.push(chars[0]);
        boolean poped = false;
        char t = 0;
        for (int i = 1; i < chars.length; i++) {
            char c = chars[i];
            if (!stack.empty() && stack.peek() == c) {
                t = stack.pop();
                poped = Boolean.TRUE;
                continue;
            }
            if (poped) {
                if (t == c) {
                    continue;
                }
                poped = false;
            }
            stack.push(c);
        }
        return stack.toString().replace(", ", "").replace("[", "").replace("]", "");
    }
}
